package org.ftz;
import org.ftz.control.util;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class test5 {
    public void writeToTxt(String content,String filename) throws IOException {
        File file1=new File("D:\\Google\\test2");
        if(!file1.exists()){
            file1.mkdir();
        }
        File file=new File("D:\\Google\\test2\\"+filename);
        FileOutputStream f1=new FileOutputStream(file);
        byte []buff=content.getBytes();
        try {
            f1.write(buff);
        }catch (Exception e){
        }finally {
            f1.close();
        }
    }
    @Test
    public void test() throws IOException {
        List<String> list = util.getZipFileList("D:\\Google\\test\\1.zip");
        String input=list.get(0).substring(0,list.get(0).length()-1);
        System.out.println(input);
        String output=list.get(1).substring(0,list.get(1).length()-1);
        System.out.println("----");
        System.out.println(output);
        String[] split1 = input.split("@");
        for(int i=0;i<split1.length;i++){
            String filename="input"+(i+1)+".txt";
            writeToTxt(split1[i],filename);
        }
        String[] split2 = output.split("@");
        for(int i=0;i<split2.length;i++){
            String filename="output"+(i+1)+".txt";
            writeToTxt(split2[i],filename);
        }
    }
    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.exists() && file.isFile()) {  // 如果文件路径只有单个文件
            if (file.delete()) {
                System.out.println("删除文件" + fileName + "成功！");
                return true;
            } else {
                System.out.println("删除文件" + fileName + "失败！");
                return false;
            }
        } else {
            System.out.println(fileName + "不存在！");
            return false;
        }
    }
    public static boolean deleteAllFile(String dir) {
        File dirFile = new File(dir);
        if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
            System.out.println("删除文件夹失败：" + dir + "不存在！");
            return false;
        }
        boolean flag = true;
        File[] files = dirFile.listFiles();  // 删除文件夹中的所有文件包括子文件夹
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) { // 删除子文件
                flag =deleteFile(files[i].getAbsolutePath());
                if (!flag) break;
            }
            else if (files[i].isDirectory()) { // 删除子文件夹
                flag = deleteAllFile(files[i].getAbsolutePath());
                if (!flag) break;
            }
        }
        if (!flag) {
            System.out.println("删除文件夹失败！");
            return false;
        }
        if (dirFile.delete()) {  // 删除当前文件夹
            System.out.println("删除文件夹" + dir + "成功！");
            return true;
        } else {
            return false;
        }
    }
    @Test
    public void test02(){
        deleteAllFile("D:\\test");
    }
    @Test
    public void test03() throws IOException {
        List<String> zipFileList = util.getZipFileList("D:\\Google\\蓝桥杯\\算法训练\\Car的旅行路线\\Car的旅行路线.zip");
        System.out.println(zipFileList.get(0));
        System.out.println(zipFileList.get(1));
    }
}
